#!/usr/bin/python
#coding=utf-8
import urllib, urllib2, socket, os, re, net_traffic, work_port
from file import montior_file
from process_total import process_list
from CPU_LOAD import cpu_load, cpu_loadcore
from mem import meminfo 
from disk import disk_usage, disk_io
import multiprocessing
import threading, get_city_name

#提交函数
def post(url, data):
    data = urllib.urlencode(data)
    req = urllib2.Request(url, data)
    response = urllib2.urlopen(req, timeout=5)
    return response.read()

def localdict(key=None,value=None):
    ret = {}
    if  key != None  and  value != None :
        ret[key] = value
    return  ret

def _out_dict(_out_dict):
    _dic = {}
    _dic = dict(_dic,**_out_dict)
    return _dic

return_val = {}
class add_out_dict(threading.Thread):
    def __init__(self, out_dict):
        threading.Thread.__init__(self)
        self.out_dict = out_dict
    def run(self):
        global return_val
        return_val = dict(return_val,**(_out_dict(self.out_dict)))




#入口函数
def main():  
    #posturl = "http://127.0.0.1:8109/recv/server"
    posturl = "http://127.0.0.1:8102/recv/server"

    #多进程处理自定义key
    pool_1 = multiprocessing.Pool(processes=5)

    List_1=[]
    List_1.append(pool_1.apply_async(localdict, args=("school","和盛大厦")))
    List_1.append(pool_1.apply_async(localdict, args=("hostname",'debian')))
    List_1.append(pool_1.apply_async(localdict, args=("memload",meminfo.virtual_memory())))
    List_1.append(pool_1.apply_async(localdict, args=("procnr",process_list())))
    List_1.append(pool_1.apply_async(localdict, args=("cpuload",cpu_loadcore.cpu_post())))
    List_1.append(pool_1.apply_async(localdict, args=("city",get_city_name.city())))

    data={}
    for res in  List_1:
        data=dict(data,**res.get())
    pool_1.close()
    pool_1.join()

    #多线程添加外部处理完的字典值
    yuanzu = ( montior_file._dict,			#文件监控
       		net_traffic.interface_dict,		#网络流量
		work_port.port_zu,			#端口监控
		disk_usage._dict,			#硬盘使用率	
		disk_io._disk_io_dict )			#硬盘IO
    for i in yuanzu:
    	thread = add_out_dict(i)
        thread.start()
    	thread.join()
          

    data=dict(data,**return_val)

    print post(posturl, data)

if __name__ == '__main__':
    main()

